<?php
/**
 * Created by IntelliJ IDEA.
 * User: XD
 * Date: 2017/8/24
 * Time: 11:23
 */
class executionSql extends BaseMysql
{
    private $basemysql;
    private $_builder;
    private $model;

    /**
     * @return mixed
     */
    public function getBasemysql()
    {
        return $this->basemysql;
    }

    /**
     * @param mixed $basemysql
     */
    public function setBasemysql($basemysql)
    {
        $this->basemysql = $basemysql;
    }


    /**
     * executionSql constructor.
     * @param $_builder
     * @param $model
     */
    function __construct($_builder, $model)
    {
        $this->_builder = $_builder;
        $this->model = $model;
        $basemysql = parent::getInstance($_builder,$model);
        if($basemysql == null){
            Testlog::addInfoAtLog('获取basemysql的对象失败',__FILE__,__LINE__);
            die("获取basemysql的对象失败");
        }else{
            $this->setBasemysql($basemysql);
        }


    }

    function executionSql($dbname,$sql){
        if(parent::getMode() === 1){
            $this->executionSqlMysqliProcess($dbname,$sql);
        }else if(parent::getMode() === 2){
            $this->executionSqlMysqliObject($sql);
        }else if(parent::getMode() === 3){
            $this->executionSqlPDO($sql);
        }else{
            Testlog::addInfoAtLog("设置的链接模式没有匹配到，请检查输入的链接模式是否是1，2，3 的整数",__FILE__,__LINE__);
            die("设置的链接模式没有匹配到，请检查输入的链接模式是否是1，2，3 的整数");
        }
    }

    function executionMoreSql($arraysql){
        if(parent::getMode() === 1){
            $this->executionSqlMoreMysqliProcess($arraysql);
        }else if(parent::getMode() === 2){
            $this->executionSqlMoreMysqliObject($arraysql);
        }else if(parent::getMode() === 3){
            $this->executionMoreSqlPDO($arraysql);
        }else{
            Testlog::addInfoAtLog("设置的链接模式没有匹配到，请检查输入的链接模式是否是1，2，3 的整数",__FILE__,__LINE__);
            die("设置的链接模式没有匹配到，请检查输入的链接模式是否是1，2，3 的整数");
        }
    }

    private function executionSqlMysqliObject($sql){
        if($this->getBasemysql()->getConn() != null){
            //print_r($this->getBasemysql()->getConn());  验证对象
            if($this->getBasemysql()->getConn()->query($sql) === TRUE){
                Testlog::addInfoAtLog("sql 语句执行成功".$sql,__FILE__,__LINE__);
            }else{
                Testlog::addInfoAtLog("sql 语句执行失败: ".$sql.":".$this->getBasemysql()->getConn()->error.";maybe database already exists",__FILE__,__LINE__);
            }
        }else{
            Testlog::addInfoAtLog("数据库对象 conn 为null 请检查",__FILE__,__LINE__);
        }
    }

    private function executionSqlMoreMysqliObject($arraysql){
        /**
         * $sql = "INSERT INTO MyGuests (firstname, lastname, email)
        VALUES ('John', 'Doe', 'john@example.com');";
        $sql .= "INSERT INTO MyGuests (firstname, lastname, email)
        VALUES ('Mary', 'Moe', 'mary@example.com');";
        $sql .= "INSERT INTO MyGuests (firstname, lastname, email)
        VALUES ('Julie', 'Dooley', 'julie@example.com')";
         * $arraysql 格式如上面所示;
         */
        if($this->getBasemysql()->getConn() != null){
            //print_r($this->getBasemysql()->getConn());  验证对象
            if($this->getBasemysql()->getConn()->multi_query($arraysql) === TRUE){
                Testlog::addInfoAtLog("sql 语句执行成功".$arraysql,__FILE__,__LINE__);
            }else{
                Testlog::addInfoAtLog("sql 语句执行失败: ".$arraysql.":".$this->getBasemysql()->getConn()->error.";maybe database already exists",__FILE__,__LINE__);
            }
        }else{
            Testlog::addInfoAtLog("数据库对象 conn 为null 请检查",__FILE__,__LINE__);
        }
    }

    /**
     * @param $sql
     * @param $firstname
     * @param $lastname
     * @param $email
     */
    private function executionSqlPretreatmentMysqliObject($sql, $firstname,$lastname, $email){
        if($this->getBasemysql()->getConn() != null){
            //print_r($this->getBasemysql()->getConn());  验证对象
            // 为  mysqli_stmt_prepare() 初始化 statement 对象
            $stmt = mysqli_stmt_init($this->getBasemysql()->getConn());
            //预处理语句
            if (mysqli_stmt_prepare($stmt, $sql)) {
                // 绑定参数 后面多少个参数 第二个就多少个 s
                mysqli_stmt_bind_param($stmt, 'sss', $firstname, $lastname, $email);

                // 设置参数并执行
                $firstname = 'John';
                $lastname = 'Doe';
                $email = 'john@example.com';
                if(mysqli_stmt_execute($stmt)){
                    Testlog::addInfoAtLog("sql 语句执行成功".$stmt,__FILE__,__LINE__);
                }

                $firstname = 'Mary';
                $lastname = 'Moe';
                $email = 'mary@example.com';
                if(mysqli_stmt_execute($stmt)){
                    Testlog::addInfoAtLog("sql 语句执行成功".$stmt,__FILE__,__LINE__);
                }

                $firstname = 'Julie';
                $lastname = 'Dooley';
                $email = 'julie@example.com';
                if(mysqli_stmt_execute($stmt)){
                    Testlog::addInfoAtLog("sql 语句执行成功".$stmt,__FILE__,__LINE__);
                }
            }else{
                Testlog::addInfoAtLog("预处理执行失败: ",__FILE__,__LINE__);
            }
        }else{
            Testlog::addInfoAtLog("数据库对象 conn 为null 请检查",__FILE__,__LINE__);
        }
    }

    private function executionSqlMysqliProcess($dbname,$sql){
        if($this->basemysql->getConn() == null){
            Testlog::addInfoAtLog("获取conn对象失败",__FILE__,__LINE__);
        }
        $this->selectDBMysqliProcess($this->basemysql->getConn(),$dbname);
        // 创建数据库
        if($this->getBasemysql()->getConn() != null){
            //print_r($this->getBasemysql()->getConn());  //验证对象
            if(mysqli_query($this->getBasemysql()->getConn(),$sql)){
                Testlog::addInfoAtLog("sql 语句执行成功".$sql,__FILE__,__LINE__);
            }else{
                Testlog::addInfoAtLog("sql 语句执行失败: ".$sql.":".mysqli_error($this->getBasemysql()->getConn())."maybe database already exists",__FILE__,__LINE__);
            }
        }else{
            Testlog::addInfoAtLog("数据库连接对象 conn为null",__FILE__,__LINE__);
        }
    }

    private function executionSqlMoreMysqliProcess($arraysql){
        /**
         * $sql = "INSERT INTO MyGuests (firstname, lastname, email)
        VALUES ('John', 'Doe', 'john@example.com');";
        $sql .= "INSERT INTO MyGuests (firstname, lastname, email)
        VALUES ('Mary', 'Moe', 'mary@example.com');";
        $sql .= "INSERT INTO MyGuests (firstname, lastname, email)
        VALUES ('Julie', 'Dooley', 'julie@example.com')";
         * $arraysql 格式如上面所示;
         */
        // 创建数据库
        if($this->getBasemysql()->getConn() != null){
            //print_r($this->getBasemysql()->getConn());  //验证对象
            if(mysqli_multi_query($this->getBasemysql()->getConn(),$arraysql)){
                Testlog::addInfoAtLog("sql 语句执行成功".$arraysql,__FILE__,__LINE__);
            }else{
                Testlog::addInfoAtLog("sql 语句执行失败: ".$arraysql.":".mysqli_error($this->getBasemysql()->getConn())."maybe database already exists",__FILE__,__LINE__);
            }
        }else{
            Testlog::addInfoAtLog("数据库连接对象 conn为null",__FILE__,__LINE__);
        }
    }


    private function executionSqlPDO($sql){
        try{
            $this->getBasemysql()->getConn()->exec($sql);
            Testlog::addInfoAtLog("此条sql执行成功：".$sql,__FILE__,__LINE__);
        }catch (PDOException $e){
            Testlog::addInfoAtLog($sql." err:".$e->getMessage(),__FILE__,__LINE__);
        }
    }

    //pdo 多事务提交
    private function executionMoreSqlPDO($arraysql){
        try{
            //开始事务
            $this->getBasemysql()->getConn()->beginransaction();
            //SQL 语句
            foreach ($arraysql as $sql) {
                $this->getBasemysql()->getConn()->exec($sql);

            }
            $this->getBasemysql()->getConn()->commit();
            Testlog::addInfoAtLog("sql数组执行成功：".$arraysql,__FILE__,__LINE__);
        }catch (PDOException $e){
            // 如果执行失败回滚
            $this->getBasemysql()->getConn()->rollback();
            Testlog::addInfoAtLog($arraysql." err:".$e->getMessage(),__FILE__,__LINE__);
        }
    }


}
